﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Collections.Specialized;
using System.Collections;

namespace Doo.WebUI.SiteBuilder.PropertyEditor
{
    public class EditorPanelItem
    {
        public string PropertyName { get; set; }

        public string TitleName { get; set; }

        public EditorBase Editor { get; set; }
    }

    /// <summary>
    /// 编辑器面板，是一个依附于WidgetControl的属性编辑面板。与typeof(WidgetControl)对应，不与Instance对应。
    /// </summary>
    public class EditorPanel : Doo.DataView.DataViewBase
    {
        public override string ClientControlType
        {
            get
            {
                return "Doo.PageBuilder.EditorPanel";
            }
        }

        public override string ScriptResourceName
        {
            get
            {
                return "Doo.WebUI.SiteBuilder.PropertyEditor.Editor.js";
            }
        }

        List<EditorPanelItem> itemlist = new List<EditorPanelItem>();

        public void AddItem(EditorPanelItem item)
        {
            itemlist.Add(item);
        }

        public void AddItem(string propertyName, EditorBase editor)
        {
            itemlist.Add(new EditorPanelItem { PropertyName = propertyName, TitleName = propertyName, Editor = editor });
        }

        protected HashSet<string> resourceName
        {
            get
            {
                var rn = Page.Items["Doo.EditorResource"] as HashSet<string>;
                if (rn == null)
                {
                    rn = new HashSet<string>();
                    Page.Items["Doo.EditorResource"] = rn;
                }
                return rn;
            }
        }

        protected override void SetScriptReferences(List<System.Web.UI.ScriptReference> reference)
        {
            base.SetScriptReferences(reference);

            foreach (var item in itemlist)
            {
                string rn = item.Editor.ScriptResourceName;
                if (!resourceName.Contains(rn))
                {
                    resourceName.Add(rn);
                    reference.Add(item.Editor.GetScriptReference());
                }
            }
        }

        protected override void SetScriptDescriptor(ScriptControlDescriptor descriptor)
        {
            base.SetScriptDescriptor(descriptor);

            //添加items
            List<clientitem> clientitemlist = new List<clientitem>();
            foreach (var item in itemlist)
            {
                clientitemlist.Add(new clientitem { field = item.PropertyName, title = item.TitleName, type = item.Editor.ClientType, option = item.Editor.Option });
            }

            descriptor.AddProperty("properties", clientitemlist);
        }

        class clientitem
        {
            public string field;
            public string title;
            public string type;
            public object option;
        }
    }
}
